﻿// <copyright file="ColorToNamedColorConverter.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-06-13</date>
// <summary>OpenWPFChart library.
// KnownColorPicker User Control. ColorToNamedColor Value Converter.</summary>
// <revision>$Id$</revision>

using System;
using System.Windows.Data;
using System.Windows.Media;

namespace OpenWPFChart.Helpers.ChooseColorDialog
{
	/// <summary>
	/// Converts the <see cref="Color"/> to the <see cref="KnownColorPickerViewModel.NamedColor"/> value.
	/// </summary>
	[ValueConversion(typeof(Color), typeof(KnownColorPickerViewModel.NamedColor))]
	class KnownColorPickerColorToNamedColorConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Converts the Color specified in <paramref name="value"/> to 
		/// the <see cref="KnownColorPickerViewModel.NamedColor"/>.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns></returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || value.GetType() != typeof(Color))
				return value;
			Color color = (Color)value;
			foreach (KnownColorPickerViewModel.NamedColor item in KnownColorPickerViewModel.WellKnownColors)
			{
				if (item.Color == color)
					return item;
			}
			return null;
		}

		/// <summary>
		/// Converts the specified <see cref="KnownColorPickerViewModel.NamedColor"/> to the Color.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || value.GetType() != typeof(KnownColorPickerViewModel.NamedColor))
				return value;
			return ((KnownColorPickerViewModel.NamedColor)value).Color;
		}
		#endregion IValueConverter Members
	}
}
